 package com.neusoft.elm.View.impl;

import java.util.List;
import java.util.Scanner;

import com.neusoft.elm.View.BusinessView;
import com.neusoft.elm.dao.BusinessDao;
import com.neusoft.elm.dao.impl.BusinessDaoImpl;
import com.neusoft.elm.po.Business;

public class BusinessViewImpl implements BusinessView {
	
	private Scanner input = new Scanner(System.in);

	@Override
	public void listBusinessAll() {
		
		BusinessDao dao = new BusinessDaoImpl();
		List<Business> list = dao.listBusiness(null,null);
		
		System.out.println("商家编号\t密码\t商家名称\t商家地址\t商家介绍\t起送费\t配送费");
		for(Business b:list){
			System.out.println(b.getBusinessId()+"\t"+b.getPassword()+"\t"
		            +b.getBusinessName()+"\t"+b.getBusinessAddress()+"\t"
		            +b.getBusinessExplain()+"\t"+b.getStarPrice()+"\t"
	            	+b.getDeliveryPrice());
		}
	}

	@Override
	public void listBusiness() {
		String businessName = "";
		String businessAddress = "";
		
		String inputStr = "";
		System.out.println("是否以商家名称为关键字进行搜索(y/n)");
		inputStr = input.next();
		if(inputStr.equals("y")){
			System.out.println("请输入商家名称：");
			businessName = input.next();
		}
		
		System.out.println("是否以商家地址为关键字进行搜索(y/n)");
		inputStr = input.next();
		if(inputStr.equals("y")){
			System.out.println("请输入商家地址：");
			businessAddress = input.next();
		}
		
		

		
		BusinessDao dao = new BusinessDaoImpl();
		List<Business> list = dao.listBusiness(businessName,businessAddress);
		
		System.out.println("商家编号\t密码\t商家名称\t商家地址\t商家介绍\t起送费\t配送费");
		for(Business b:list){
			System.out.println(b.getBusinessId()+"\t"+b.getPassword()+"\t"
		            +b.getBusinessName()+"\t"+b.getBusinessAddress()+"\t"
		            +b.getBusinessExplain()+"\t"+b.getStarPrice()+"\t"
	            	+b.getDeliveryPrice());
		}
	
	}

	@Override
	public void saveBusiness() {
		System.out.println("请输入商家名称：");
		String businessName = input.next();
		BusinessDao dao = new BusinessDaoImpl();
		int businessId = dao.saveBusiness(businessName);
		if(businessId>0){
			System.out.println("新建商家成功！商家编号为："+businessId);
		}else{
			System.out.println("新建商家失败！请确认信息合法性");
			}
		
	}

	@Override
	public void removeBusiness() {
		System.out.println("请输入商家编号：");
		int businessId = input.nextInt();
		BusinessDao dao = new BusinessDaoImpl();
		System.out.println("是否确认删除？(y/n)");
		if(input.next().equals("y")){
			int result = dao.removeBusiness(businessId);
			if(result>0){
				System.out.println("删除成功");
			}else{
				System.out.println("删除失败");
			}
		}
		
	}

	@Override
	public Business login() {

		System.out.println("请输入商家编号：");
		int businessId = input.nextInt();
		System.out.println("请输入密码：");
		String password = input.next();
		BusinessDao dao = new BusinessDaoImpl();
		return dao.getBusinessByIdByPass(businessId, password);

	}

	@Override
	public void showBusiness(Integer businessId) {
		BusinessDao dao = new BusinessDaoImpl();
		Business business = dao.showBusiness(businessId);
		System.out.println(business);
	}

	@Override
	public void editBusiness(Integer businessId) {
		BusinessDao dao = new BusinessDaoImpl();
		Business business = dao.showBusiness(businessId);
		System.out.println(business);
		String inputstr = "";
		System.out.println("是否修改商家名称？(y/n)");
		inputstr = input.next();
		if(inputstr.equals("y")){
			System.out.println("请输入新的商家名称：");
			/*String str = input.next();
			business.setBusinessName(str);*/
			//上两行代码简化：
			business.setBusinessName(input.next());
		}
		System.out.println("是否修改商家地址？(y/n)");
		inputstr = input.next();
		if(inputstr.equals("y")){
			System.out.println("请输入新的商家地址：");
			business.setBusinessAddress(input.next());
		}
		System.out.println("是否修改商家介绍？(y/n)");
		inputstr = input.next();
		if(inputstr.equals("y")){
			System.out.println("请输入新的商家介绍：");
			business.setBusinessExplain(input.next());
		}
		System.out.println("是否修改起送费？(y/n)");
		inputstr = input.next();
		if(inputstr.equals("y")){
			System.out.println("请输入新的起送费：");
			business.setStarPrice(input.nextDouble());
		}
		System.out.println("是否修改配送费？(y/n)");
		inputstr = input.next();
		if(inputstr.equals("y")){
			System.out.println("请输入新的配送费：");
			business.setDeliveryPrice(input.nextDouble());
		}
		
		int result = dao.updateBusiness(business);
		if(result>0){
			System.out.println("修改商家信息成功");
		}else{
			System.out.println("修改商家信息失败");
		}
	}

	@Override
	public void updateBusinessByPassword(Integer businessId) {
		BusinessDao dao = new BusinessDaoImpl();
		Business business = dao.showBusiness(businessId);
		System.out.println("请输入旧密码");
		String oldPass = input.next();
		System.out.println("请输入新密码");
		String password = input.next();
		System.out.println("请再次输入新密码");
		String password2 = input.next();
		//if条件取反
		if(!business.getPassword().equals(oldPass)){
			System.out.println("旧密码输入错误！请确认后重新输入！");
		}else if(!password.equals(password2)){
			System.out.println("两次输入密码不一致，请确认后重新输入！");
		}else{
			//验证完毕后，调用Dao层里更新密码的方法
			int result = dao.updateBusinessByPassword(businessId, password);
			if(result>0){
				System.out.println("密码更新成功！");
			}else{
				System.out.println("密码更新失败！");
			}
		}
	}

}
